/***************************************************************************
 * The Open Armory API is a project to retrieve World of Warcraft(R) 
 * related XML data from The Armory.
 * 
 * The data retrieved via the Open Armory API from The Armory is 
 * proprietary to Blizzard Entertainment, Inc(R).
 * *************************************************************************
 * Copyright (C) 2009 - 2010 Tedla M.B. Brandsema
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ***************************************************************************/
package net.openarmory.core.domain;

import java.io.InputStream;

import net.openarmory.core.domain.constants.ArmoryContentGroup;
import net.openarmory.core.domain.constants.ArmoryGuildDataType;
import net.openarmory.core.domain.constants.ArmoryZone;
import net.openarmory.core.domain.url.ArmoryGuildUrl;
import net.openarmory.core.domain.url.ArmoryGuildUrlBuilder;
import net.openarmory.core.domain.url.ArmoryUrl;
import net.openarmory.core.domain.url.ArmoryUrlFactory;
import net.openarmory.core.domain.url.UrlConnectionReader;

/**
 * The Class {@code ArmoryGuildXmlRetriever} retrieves guild related XML from
 * The Armory.
 */
public final class ArmoryGuildXmlRetriever extends UrlConnectionReader {

	/** The guild URL builder. */
	private final ArmoryGuildUrlBuilder guildUrlBuilder;

	/** The armory URL. */
	private ArmoryUrl armoryUrl;

	/**
	 * This method returns the {@link ArmoryUrl} that is instantiated by the
	 * {@link #fetchContent(java.net.URL)} method. The method returns
	 * {@code null} if the {@link #fetchContent(java.net.URL)} has not been
	 * called first.
	 * 
	 * @return The guild specific armory URL.
	 */
	public ArmoryUrl getArmoryUrl() {
		return this.armoryUrl;
	}

	/**
	 * Instantiates a new armory guild XML retriever.
	 */
	public ArmoryGuildXmlRetriever() {
		this.guildUrlBuilder = (ArmoryGuildUrlBuilder) ArmoryUrlFactory
				.getUrlBuilder(ArmoryContentGroup.GUILD);
	}

	/**
	 * Method for fetching guild related data from The Armory guild tabs,
	 * represented here by the {@link ArmoryGuildDataType} enumeration. This
	 * method differs from
	 * {@link #fetchGuildXml(ArmoryZone, String, String, ArmoryGuildDataType)}
	 * in that this method takes the character name as a parameter. Due note
	 * that both methods return the exact same data.
	 * 
	 * @param zone
	 *            The zone on which the guild resides in (i.e. EU or US).
	 * @param realm
	 *            The realm on which the guild resides on.
	 * @param characterName
	 *            The character name.
	 * @param guild
	 *            The guild name.
	 * @param armoryGuildDataType
	 *            The armory guild data type.
	 * @return The {@code InputStream} containing XML data from the requested
	 *         URL.
	 */
	public InputStream fetchCharacterGuildXml(final ArmoryZone zone,
			final String realm, final String characterName, final String guild,
			final ArmoryGuildDataType armoryGuildDataType) {
		final ArmoryGuildUrl guildUrl = this.guildUrlBuilder
				.getCharacterGuildUrl(zone, realm, characterName, guild,
						armoryGuildDataType);
		this.armoryUrl = guildUrl;
		return fetchContent(guildUrl.getUrl());
	}

	/**
	 * Method for fetching guild related data from The Armory guild tabs,
	 * represented here by the {@link ArmoryGuildDataType} enumeration. This
	 * method differs from
	 * {@link #fetchCharacterGuildXml(ArmoryZone, String, String, String, ArmoryGuildDataType)}
	 * in that this method does not take the character name as a parameter. Due
	 * note that both methods return the exact same data.
	 * 
	 * @param zone
	 *            The zone on which the guild resides in (i.e. EU or US).
	 * @param realm
	 *            The realm on which the guild resides on.
	 * @param guild
	 *            The guild name.
	 * @param armoryGuildDataType
	 *            The armory guild data type.
	 * @return The {@code InputStream} containing XML data from the requested
	 *         URL.
	 */
	public InputStream fetchGuildXml(final ArmoryZone zone, final String realm,
			final String guild, final ArmoryGuildDataType armoryGuildDataType) {
		final ArmoryGuildUrl guildUrl = this.guildUrlBuilder.getGuildUrl(zone,
				realm, guild, armoryGuildDataType);
		this.armoryUrl = guildUrl;
		return fetchContent(guildUrl.getUrl());
	}

}
